<?php
include_once('config.php');
// Reset errors and success messages  
$errors = array();  
$success = array(); 
?>

<html>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<style TYPE="text/css">

table { 
	margin: 1em; 
	border-collapse: collapse;
	text-align: center;
	font-family: Verdana;
	font-weight: normal;
	font-size: 11px;
	}
td, th { padding: .3em; border: 0px #ccc solid; }
thead { background: #c8faab; }
tbody { background: #ffffff; }
#highlight tr.hilight { background: #dfffe1; }


td.birdie {
	background-color: #6ecdff;
}
td.eagle {
	background-color: #c4fbff;
}
td.albatross {
	background-color: #ff00f2;
}
td.bogey {
	background-color: #fcf74f;
}
td.doublebogey {
	background-color: #ffb700;
}
td.triplebogey {
	background-color: #ff0000;
}


.button {
    border: 1px solid #006;
    background: #dfffe1;
}



body {

	font-family: Helvetica, Arial, sans-serif;
	font-size: small;		}
	
.center { 
	text-align: center;
}


.centered-table {
   margin-left: auto;
   margin-right: auto;
}

#header {
  background: black url(images/header.jpg) center no-repeat;
  height: 200;
}


</style>

<script>
function tableHighlightRow() {
  if (document.getElementById && document.createTextNode) {
    var tables=document.getElementsByTagName('table');
    for (var i=0;i<tables.length;i++)
    {
      if(tables[i].className=='hilite') {
        var trs=tables[i].getElementsByTagName('tr');
        for(var j=0;j<trs.length;j++)
{
          if(trs[j].parentNode.nodeName=='TBODY') {
            trs[j].onmouseover=function(){this.className='hilight';return false}
            trs[j].onmouseout=function(){this.className='';return false}
          }
        }
      }
    }
  }
}
window.onload=function(){tableHighlightRow();}
</script>


<body>
<div id="header"></div>

<?
$tbl_name="players"; // Table name 

// Run a quick check to see if we are an authenticated user or not
// First, we set a 'is the user logged in' flag to false by default.
$isUserLoggedIn = false;
$query 		= 'SELECT * FROM users WHERE session_id = "' . session_id() . '" LIMIT 1';
$userResult 	= mysql_query($query);
if(mysql_num_rows($userResult) == 1){
	$_SESSION['user'] = mysql_fetch_assoc($userResult);
	$isUserLoggedIn = true;
}else{
	if(basename($_SERVER['PHP_SELF']) != 'index.php'){
		header('Location: index.php');
		exit;
	}
}


// Login attempt
if(isset($_POST['loginSubmit']) && $_POST['loginSubmit'] == 'true'){
	$loginEmail = trim($_POST['email']);
	$loginPassword 	= trim($_POST['password']);
	
		if (!preg_match("/^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}$/", $loginEmail))
		$errors['loginEmail'] = 'Votre adresse email ' . $loginEmail . ' est incorrecte.';
	
	if(strlen($loginPassword) < 6 || strlen($loginPassword) > 12)
		$errors['loginPassword'] = 'Votre mot de passe doit être compris entre 6 et 12 caractères.';
	
	if(!$errors){
		$query 	= 'select * from players where email = "' . mysql_real_escape_string($loginEmail) . '" and password = md5("' . $loginPassword . '") limit 1';
		$result = mysql_query($query);
		if(mysql_num_rows($result) == 1){
			$user = mysql_fetch_assoc($result);
			$query = 'UPDATE users SET session_id = "' . session_id() . '" WHERE id = ' . $user['id'] . ' LIMIT 1';
			mysql_query($query);
			header('Location: accueil.php');
			exit;
		}else{
			$errors['login'] = 'No user was found with the details provided.';
		}
	}
}

// Register attempt
if(isset($_POST['registerSubmit']) && $_POST['registerSubmit'] == 'true'){
	$registerEmail = trim($_POST['email']);
	$registerPassword = trim($_POST['password']);
	$registerConfirmPassword 	= trim($_POST['confirmPassword']);
	
	if (!eregi("^[_a-z0-9-] (.[_a-z0-9-] )*@[a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3})$", $registerEmail)) 
		$errors['registerEmail'] = 'Your email address is invalid.';
	
	if(strlen($registerPassword) < 6 || strlen($registerPassword) > 12)	
		$errors['registerPassword'] = 'Your password must be between 6-12 characters.';
	
	if($registerPassword != $registerConfirmPassword)
		$errors['registerConfirmPassword'] = 'Your passwords did not match.';
	
	// Check to see if we have a user registered with this email address already
	$query = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($registerEmail) . '" LIMIT 1';
	$result = mysql_query($query);
	if(mysql_num_rows($result) == 1) 
		$errors['registerEmail'] = 'This email address already exists.';
	
	if(!$errors){
		$query = 'INSERT INTO users SET email = "' . mysql_real_escape_string($registerEmail) . '", 
																		password = MD5("' . mysql_real_escape_string($registerPassword) . '"), 
																		date_registered = "' . date('Y-m-d H:i:s') . '"';
		
		if(mysql_query($query)){
			$success['register'] = 'Thank you for registering. You can now log in on the left.';
		}else{
			$errors['register'] = 'There was a problem registering you. Please check your details and try again.';
		}
	}
	
}
?>
  <header><h1>Login / Register Here</h1></header>
	
	<form class="box400" name="loginForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
		<h2>Identifiez-vous</h2>
		<?php if($errors['login']) print '<div class="invalid">' . $errors['login'] . '</div>'; ?>
		
		<label for="email">Email Address</label>
		<input type="text" name="email" value="<?php echo htmlspecialchars($loginEmail); ?>" placeholder = "Adresse Email" />
		<?php if($errors['loginEmail']) print '<div class="invalid">' . $errors['loginEmail'] . '</div>'; ?>
		
		<label for="password">Password <span class="info">6-12 chars</span></label>
		<input type="password" name="password" value="" placeholder="Votre mot de passe"/>
		<?php if($errors['loginPassword']) print '<div class="invalid">' . $errors['loginPassword'] . '</div>'; ?>
		
		<label for="loginSubmit">&nbsp;</label>
		<input type="hidden" name="loginSubmit" id="loginSubmit" value="true" />
		<input type="submit" value="Login" class="button" />
	</form>
	
	<form class="box400" name="registerForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
		<h2>Enregistrez-vous</h2>
		<?php if($success['register']) print '<div class="valid">' . $success['register'] . '</div>'; ?>
		<?php if($errors['register']) print '<div class="invalid">' . $errors['register'] . '</div>'; ?>
		
		<label for="email">Email Address</label>
		<input type="text" name="email" value="<?php echo htmlspecialchars($registerEmail); ?>" />
		<?php if($errors['registerEmail']) print '<div class="invalid">' . $errors['registerEmail'] . '</div>'; ?>
		
		<label for="password">Password</label>
		<input type="password" name="password" value="" />
		<?php if($errors['registerPassword']) print '<div class="invalid">' . $errors['registerPassword'] . '</div>'; ?>
		
		<label for="confirmPassword">Confirm Password</label>
		<input type="password" name="confirmPassword" value="" />
		<?php if($errors['registerConfirmPassword']) print '<div class="invalid">' . $errors['registerConfirmPassword'] . '</div>'; ?>
		
		<label for="registerSubmit">&nbsp;</label>
		<input type="hidden" name="registerSubmit" id="registerSubmit" value="true" />
		<input type="submit" value="Register" />
	</form>



</body>
</html>